import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;

/* @comany 阿斯达克(深圳)信息技术有限公司
* @author steven.wu
* @since 2009.8.11
* @description read and write stream for binary file。
*/
public class BinaryReadWrite {
	private DataInputStream dis=null;
	private DataOutputStream dos=null;
	private String s_FilePath="E:\\zd_zszq\\vipdoc\\sh\\lday\\sh000001.day";
	private byte[] m_datapadding = { 0x00 }; //填充空白，以补足字节位数.
	
	public BinaryReadWrite() {
		// TODO Auto-generated constructor stub
		init();
	}
	private void init(){
		try{
			if(!new File(s_FilePath).exists()){
				new File(s_FilePath).createNewFile();
			}
			dis=new DataInputStream(new FileInputStream(new File(s_FilePath)));
//			dos=new DataOutputStream(new FileOutputStream(new File(s_FilePath)));
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public void writeBinaryStream(){
		try{
			if(dos!=null){
				for(int i=0;i<2;i++){
					//write boolean value.
					dos.writeBoolean(true);
					//write a char value.
					char c='a';
					dos.writeChar((int)c);
					Double d=12.567d;
					dos.writeDouble(d);
					Float f=56.782f;
					dos.writeFloat(f);
					int k=105;
					dos.writeInt(k);
					long l=98765l;
					dos.writeLong(l);
					short st=12;
					dos.writeShort(st);
					String cs="Java读写二进制文件";
					String cs1="二进制文件";
					if(i==0){
						dos.writeUTF(cs);
					}else{
						dos.writeUTF(cs1);
					}
					dos.write(m_datapadding);
				}
				dos.flush();
				dos.close();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public void readBinaryStream(){
		try{
			if(dis!=null){
				while(dis.available()>0){
					System.out.println(dis.available());
					System.out.println(dis.readBoolean());
					char c=(char)dis.readChar();
					System.out.println(c);
					System.out.println(dis.readDouble());
					System.out.println(dis.readFloat());
					System.out.println(dis.readInt());
					System.out.println(dis.readLong());
					System.out.println(dis.readShort());
					System.out.println(dis.readUTF());
					System.out.println(dis.read(m_datapadding));
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	   /**    
     * 对二进制文件比较常见的类有FileInputStream，DataInputStream    
     * BufferedInputStream等。类似于DataOutputStream，DataInputStream    
     * 也提供了很多方法用于读入布尔型、字节、字符、整形、长整形、短整形、    
     * 单精度、双精度等数据。    
     */     
    public static void readMethod1()      
    {      
        String fileName="E:\\zd_zszq\\vipdoc\\sh\\lday\\sh000001.day";      
        int sum=0;      
        try     
        {      
            DataInputStream in=new DataInputStream(      
                               new BufferedInputStream(      
                               new FileInputStream(fileName)));      
            sum+=in.readInt();      
            sum+=in.readInt();      
            sum+=in.readInt();      
            System.out.println("The sum is:"+sum);      
            in.close();      
        } catch (Exception e)      
        {      
            e.printStackTrace();      
        }      
    }      
          
	
	public static void readMethod2()      
    {      
        try     
        {      
            FileInputStream stream=new FileInputStream("E:\\zd_zszq\\vipdoc\\sh\\lday\\sh000001.day");      
            int c;      
            while((c=stream.read())!=-1)      
            {      
                System.out.println(c);      
            }      
        } catch (Exception e)      
        {      
            e.printStackTrace();      
        }
    }
	
	public static void readFile(String fileName){  
        
        
        File file = new File(fileName);   
        if(file.exists()){  
            try {  
                FileInputStream in = new FileInputStream(file);  
                DataInputStream dis=new DataInputStream(in);  
                
//                while(dis.available()>0){ 
                // 日期
                String date = parseDayFileForString(dis);  
                
                //开盘价 
                BigDecimal begin = parseDayFileForAmt(dis);  
                
                //最高价
                BigDecimal high = parseDayFileForAmt(dis);  
                  
                //最低价
                BigDecimal low = parseDayFileForAmt(dis);  
                  
                //收盘价
                BigDecimal end = parseDayFileForAmt(dis);  
  
                //成交量
                BigDecimal vol = parseDayFileForAmt(dis);  
                
                //成交金额
                BigDecimal amount = parseDayFileForAmt(dis);  

                String s1 = parseDayFileForString(dis);  
                String s2 = parseDayFileForString(dis);  
                String s3 = parseDayFileForString(dis);  
                String s4 = parseDayFileForString(dis);  
                String s5 = parseDayFileForString(dis);  
                String s6 = parseDayFileForString(dis);  
                String s7 = parseDayFileForString(dis);  
                String s8 = parseDayFileForString(dis);  
                String s9 = parseDayFileForString(dis);  
                  
//                System.out.println("文件长度:"+ fileLen);  
                System.out.println("日期:"+ date);  
                System.out.println("开盘价:"+ begin);  
                System.out.println("最高价:"+ high);  
                System.out.println("最低价:"+ low);  
                System.out.println("收盘价:"+ end);  
                System.out.println("成交量:"+ vol);  
                System.out.println("成交金额:"+ amount);  
                System.out.println("s1:"+ s1);  
                System.out.println("s2:"+ s2);  
                System.out.println("s3:"+ s3);  
                System.out.println("s4:"+ s4);  
                System.out.println("s5:"+ s5);  
                System.out.println("s6:"+ s6);  
                System.out.println("s7:"+ s7);  
                System.out.println("s8:"+ s8);  
                System.out.println("s9:"+ s9);  
//                }
  
            } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }finally{  
                //close  
            }  
        }  
    }
	
	private static String parseDayFileForString(DataInputStream dis) throws IOException {
        byte[] b1 = new byte[1];
        byte[] b2 = new byte[1];
        byte[] b3 = new byte[1];
        byte[] b4 = new byte[1];
        dis.read(b1, 0, 1);
        dis.read(b2, 0, 1);
        dis.read(b3, 0, 1);
        dis.read(b4, 0, 1);
        String s1 = Snippet.bytesToHexString(b1);
        String s2 = Snippet.bytesToHexString(b2);
        String s3 = Snippet.bytesToHexString(b3);
        String s4 = Snippet.bytesToHexString(b4);
        return String.valueOf(Long.parseLong(s4 + s3 + s2 + s1, 16));
	}
	
	private static BigDecimal parseDayFileForAmt(DataInputStream dis) throws IOException {
		byte[] b1 = new byte[1];
		byte[] b2 = new byte[1];
		byte[] b3 = new byte[1];
		byte[] b4 = new byte[1];
		dis.read(b1, 0, 1);
		dis.read(b2, 0, 1);
		dis.read(b3, 0, 1);
		dis.read(b4, 0, 1);
		String s1 = Snippet.bytesToHexString(b1);
		String s2 = Snippet.bytesToHexString(b2);
		String s3 = Snippet.bytesToHexString(b3);
		String s4 = Snippet.bytesToHexString(b4);
		return new BigDecimal(Long.parseLong(s4 + s3 + s2 + s1, 16) / 100.0).setScale(2, RoundingMode.HALF_UP);
	}
	
	public static void main(String[] args) throws IOException {
//		BinaryReadWrite bin=new BinaryReadWrite();
//		bin.writeBinaryStream();
//		bin.readBinaryStream();
		
//		readMethod1();
		
		readFile("E:\\zd_zszq\\vipdoc\\sh\\lday\\sh000001.day");
		
//		System.out.println(Long.parseLong("0130b902", 16));
	}
}